let chartModel = {}

// 柱形图和折线图基础配置
let bar = {
    // 标题
    title: {
        text: '', // 标题
        left: 'center', //
        subtext: ''
    },
    // 提示框
    tooltip: {
        trigger: 'item'
    },
    // 制哪些系列不显示
    // legend: {
    //     top: '2%',
    //     left: 'center'
    // },
    // 直角坐标系内绘图网格
    grid: [{
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    }],
    // 工具栏。内置有导出图片，数据视图，动态类型切换，数据区域缩放，重置五个工具
    toolbox: {
    },
    // 坐标系X轴
    xAxis: {
        type: 'category',
        // 两端留空白
        boundaryGap: true,
        data: []
    },
    // 坐标系y轴
    yAxis: {
        type: ''
    },
    // 系列列表。每个系列通过 type 决定自己的图表类型 line(折线图) bar(柱形图) pie(饼图)
    series: []
}
// 饼图基础配置信息
let pie = {
    // 标题
    title: {
        text: '', // 标题
        left: 'center', //
        subtext: ''
    },
    // 提示框
    tooltip: {
        trigger: 'item'
    },
    // 制哪些系列不显示
    // legend: {
    //     orient: 'vertical', //
    //     left: 'left',  //
    // },
    series: [
    ]
}

let radar = {
    title: {
    },
    // legend: {

    // },
    radar: {
        // shape: 'circle',
        indicator: [

        ]
    },
    series: [{
        name: '',
        type: 'radar',
        data: [

        ]
    }]
}

let expression = {
    'bar': function (data, chartModel) {
        // 深度拷贝对象
        let bar = Object.assign({}, chartModel.barOption)
        // bar.title.text = d.text;
        let list = data.data.data
        let names = data.textFields1.map(v => {
            return v.colEn
        })
        let values = data.numberFields.map(v => {
            return v.colEn
        })

        let arr = []
        let seriesArr = []
        for (let i = 0; i < values.length; i++) {
            let each = {
                type: 'bar',
                name: values[i],
                data: []
            }
            seriesArr.push(each)
        }
        let name = names[0]
        list.forEach(v => {
            arr.push(v[name])
            for (let j = 0; j < seriesArr.length; j++) {
                let s = seriesArr[j]
                let colName = s.name
                let val = v[colName]
                if (val === '' || val === ' ') {
                    val = 0
                }
                s.data.push(val)
                seriesArr[j] = s
            }
        })

        bar.xAxis.data = arr
        bar.yAxis.type = 'value'
        bar.series = seriesArr
        return bar
    },
    'barLeft': function (data, chartModel) {
        let opt = this['bar'](data, chartModel)
        let val = opt.xAxis
        opt.xAxis = opt.yAxis
        opt.yAxis = val
        return opt
    },
    'barStack': function (data, chartModel) { // 堆叠图
        let opt = this['bar'](data, chartModel)
        let arr = opt.series.map(v => {
            // v.stack = 'total'
            return v
        })
        opt.series = arr
        return opt
    },
    'pie': function (data, chartModel) {
        let pie = Object.assign({}, chartModel.pieOption)
        let list = data.data.data
        let name = data.textFields1[0].colEn
        let value = data.numberFields[0].colEn
        let s = {}

        s.radius = '50%'
        let arr = list.map(v => {
            let a = {}
            a.name = v[name]
            a.value = v[value]
            if (v[value] === '') {
                a.value = 0
            }
            return a
        })

        s.data = arr
        s.type = 'pie'
        pie.series = []
        pie.series.push(s)
        return pie
    },
    'pieRose': function (data, chartModel) { // 南丁格尔玫瑰图
        let opt = this['pie'](data, chartModel)
        let s = opt.series[0]
        let newSeries = {}

        newSeries.radius = [50, 250]
        newSeries.center = ['50%', '50%']
        newSeries.roseType = 'area'
        newSeries.itemStyle = { borderRadius: 8 }
        newSeries.type = s.type
        newSeries.data = s.data
        opt.series[0] = newSeries

        return opt
    },
    'pieRing1': function (data, chartModel) {
        let opt = this['pie'](data, chartModel)
        let s = opt.series[0]
        let newSeries = {}

        newSeries.lable = {
            show: false,
            position: 'center'
        }
        newSeries.radius = ['40%', '70%']
        newSeries.type = s.type
        newSeries.data = s.data
        opt.series[0] = newSeries

        return opt
    },
    'pieRing2': function (data, chartModel) { // 环形饼图
        let opt = this['pie'](data, chartModel)
        let s = opt.series[0]
        let newSeries = {}

        newSeries.lable = {
            show: false,
            position: 'center'
        }
        newSeries.radius = ['40%', '70%']
        newSeries.type = s.type
        newSeries.data = s.data
        newSeries.itemStyle = {
            borderRadius: 10,
            borderColor: '#fff',
            borderWidth: 2
        }
        opt.series[0] = newSeries

        return opt
    },
    'line': function (data, chartModel) {
        let opt = this['bar'](data, chartModel)
        let arr = opt.series
        let sseriesArr = arr.map(s => {
            s.type = 'line'
            return s
        })

        opt.series = sseriesArr

        return opt
    },
    'lineArea': function (data, chartModel) {
        let opt = this['bar'](data, chartModel)
        let arr = opt.series
        let series = arr.map(s => {
            s.type = 'line'
            s.areaStyle = {}

            return s
        })

        opt.series = series

        return opt
    },
    'lineStack': function (data, chartModel) { // 折线图堆叠
        let opt = this['bar'](data, chartModel)
        let arr = opt.series
        let sseriesArr = arr.map(s => {
            s.type = 'line'
            s.stack = '总量'

            return s
        })

        opt.series = sseriesArr

        return opt
    },
    'lineStackArea': function (data, chartModel) { // 堆叠区域图
        let opt = this['bar'](data, chartModel)
        let arr = opt.series
        let sseriesArr = arr.map(s => {
            s.type = 'line'
            s.stack = '总量'
            s.areaStyle = {}
            s.emphasis = {
                focus: 'series'
            }
            return s
        })
        opt.series = sseriesArr
        return opt
    },
    'scatter': function (data, chartModel) {
        let scatter = Object.assign({}, chartModel.radarOption)
    },
    'radar': function (data, chartModel) {
        let radar = Object.assign({}, chartModel.radarOption)
        let list = data.data.data
        let names = data.textFields1.map(v => {
            return v.colEn
        })
        let values = data.numberFields.map(v => {
            return v.colEn
        })
        let indicator = []
        let arr = []
        values.forEach(v => {
            let a = list.forEach(k => {
                arr.push(k[v])
            })
        })
        let max = Math.max.apply(null, arr)

        let name = names[0]

        let series = []
        list.forEach(v => {
           let a = {}
           a.name = v[name]
           a.max = max
           indicator.push(a)
        })
        let valName = data.numberFields[0].showName
        let seriesData = {}
        seriesData.type = 'radar'
        seriesData.name = valName
        seriesData.data = []

        data.numberFields.forEach(v => {
            let seriesEach = {}
            seriesEach.name = v.showName
            let valueArr = []
            list.forEach(k => {
                valueArr.push(k[v.colEn])
            })
            seriesEach.value = valueArr
            seriesData.data.push(seriesEach)
        })

        series.push(seriesData)
        radar.radar.indicator = indicator
        radar.series = series

        return radar
    },
    'table': function (data, chartModel) {
        let opt = {}
        opt.xAxis = []
        opt.yAxis = []
        return opt
    }
}

chartModel.barOption = bar
chartModel.pieOption = pie
chartModel.radarOption = radar

let setEchartsOption = (function (chartModel, expression) {
    let SetEchartsOption = function () {
    }

    let obj = new SetEchartsOption()

    let fn = SetEchartsOption.prototype

    // 通过数据生成echarts配置
    fn.readData = function (type, data) {
        let option = expression[type].call(expression, data, chartModel)

        return option
    }

    return obj
})(chartModel, expression)

export default {
    setEchartsOption
}
